#include "user_crc.h"

const unsigned short crc_ta[256] = {
	0x0000, 0x1621, 0x2C42, 0x3A63, 0x5884, 0x4EA5, 0x74C6, 0x62E7,
	0xB108, 0xA729, 0x9D4A, 0x8B6B, 0xE98C, 0xFFAD, 0xC5CE, 0xD3EF,
	0x7431, 0x6210, 0x5873, 0x4E52, 0x2CB5, 0x3A94, 0x00F7, 0x16D6,
	0xC539, 0xD318, 0xE97B, 0xFF5A, 0x9DBD, 0x8B9C, 0xB1FF, 0xA7DE,
	0xE862, 0xFE43, 0xC420, 0xD201, 0xB0E6, 0xA6C7, 0x9CA4, 0x8A85,
	0x596A, 0x4F4B, 0x7528, 0x6309, 0x01EE, 0x17CF, 0x2DAC, 0x3B8D,
	0x9C53, 0x8A72, 0xB011, 0xA630, 0xC4D7, 0xD2F6, 0xE895, 0xFEB4,
	0x2D5B, 0x3B7A, 0x0119, 0x1738, 0x75DF, 0x63FE, 0x599D, 0x4FBC,
	0xC6E5, 0xD0C4, 0xEAA7, 0xFC86, 0x9E61, 0x8840, 0xB223, 0xA402,
	0x77ED, 0x61CC, 0x5BAF, 0x4D8E, 0x2F69, 0x3948, 0x032B, 0x150A,
	0xB2D4, 0xA4F5, 0x9E96, 0x88B7, 0xEA50, 0xFC71, 0xC612, 0xD033,
	0x03DC, 0x15FD, 0x2F9E, 0x39BF, 0x5B58, 0x4D79, 0x771A, 0x613B,
	0x2E87, 0x38A6, 0x02C5, 0x14E4, 0x7603, 0x6022, 0x5A41, 0x4C60,
	0x9F8F, 0x89AE, 0xB3CD, 0xA5EC, 0xC70B, 0xD12A, 0xEB49, 0xFD68,
	0x5AB6, 0x4C97, 0x76F4, 0x60D5, 0x0232, 0x1413, 0x2E70, 0x3851,
	0xEBBE, 0xFD9F, 0xC7FC, 0xD1DD, 0xB33A, 0xA51B, 0x9F78, 0x8959,
	0x9BEB, 0x8DCA, 0xB7A9, 0xA188, 0xC36F, 0xD54E, 0xEF2D, 0xF90C,
	0x2AE3, 0x3CC2, 0x06A1, 0x1080, 0x7267, 0x6446, 0x5E25, 0x4804,
	0xEFDA, 0xF9FB, 0xC398, 0xD5B9, 0xB75E, 0xA17F, 0x9B1C, 0x8D3D,
	0x5ED2, 0x48F3, 0x7290, 0x64B1, 0x0656, 0x1077, 0x2A14, 0x3C35,
	0x7389, 0x65A8, 0x5FCB, 0x49EA, 0x2B0D, 0x3D2C, 0x074F, 0x116E,
	0xC281, 0xD4A0, 0xEEC3, 0xF8E2, 0x9A05, 0x8C24, 0xB647, 0xA066,
	0x07B8, 0x1199, 0x2BFA, 0x3DDB, 0x5F3C, 0x491D, 0x737E, 0x655F,
	0xB6B0, 0xA091, 0x9AF2, 0x8CD3, 0xEE34, 0xF815, 0xC276, 0xD457,
	0x5D0E, 0x4B2F, 0x714C, 0x676D, 0x058A, 0x13AB, 0x29C8, 0x3FE9,
	0xEC06, 0xFA27, 0xC044, 0xD665, 0xB482, 0xA2A3, 0x98C0, 0x8EE1,
	0x293F, 0x3F1E, 0x057D, 0x135C, 0x71BB, 0x679A, 0x5DF9, 0x4BD8,
	0x9837, 0x8E16, 0xB475, 0xA254, 0xC0B3, 0xD692, 0xECF1, 0xFAD0,
	0xB56C, 0xA34D, 0x992E, 0x8F0F, 0xEDE8, 0xFBC9, 0xC1AA, 0xD78B,
	0x0464, 0x1245, 0x2826, 0x3E07, 0x5CE0, 0x4AC1, 0x70A2, 0x6683,
	0xC15D, 0xD77C, 0xED1F, 0xFB3E, 0x99D9, 0x8FF8, 0xB59B, 0xA3BA,
	0x7055, 0x6674, 0x5C17, 0x4A36, 0x28D1, 0x3EF0, 0x0493, 0x12B2
};

unsigned short crc_cal(void* data, unsigned int len)
{
    len -= 2;
    unsigned char* ptr     = (unsigned char*)data;
    unsigned short crc	   = 0;
    unsigned short current = 0;
    crc = 0;
    while (len--)
    {
        current = (unsigned short)crc >> 8;
        crc <<= 8;
        crc ^= crc_ta[current ^ *ptr++];
    }
    crc = crc << 8 | crc >> 8;
    return crc;
}

int crc_check(void* data, int len)
{
    unsigned char* ptr     = (unsigned char*)data;
    unsigned short crc     = 0;
    unsigned short current = 0;
    crc = 0;
    while (len--)
    {
        current = (unsigned short)crc >> 8;
        crc <<= 8;
        crc ^= crc_ta[current ^ *ptr++];
    }
    return crc ? CRC_FALSE : CRC_TURE;
}



